/*
ont-space - The ontology-based resource metadata repository
Copyright (c) 2006-2008, Information Eng. Research Unit - Univ. of Alcalá
http://www.cc.uah.es/ie
This library is free software; you can redistribute it and/or modify it under
the terms of the GNU Lesser General Public License as published by the Free
Software Foundation; either version 2.1 of the License, or (at your option)
any later version.
This library is distributed in the hope that it will be useful, but WITHOUT
ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
details.
You should have received a copy of the GNU Lesser General Public License along
with this library; if not, write to the Free Software Foundation, Inc.,
59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
 */
package org.ontspace.owl.util;

import java.io.File;
import java.io.IOException;
import java.net.URI;
import java.net.URISyntaxException;
import java.util.HashMap;
import java.util.List;
import org.jdom.Document;
import org.jdom.Element;
import org.jdom.JDOMException;
import org.jdom.input.SAXBuilder;
import org.ontspace.util.Configuration;

public class OspaceConfiguration implements Configuration {

    /** HashMap with the general parameters for the repository */
    protected HashMap _params;
    /** HashMap with the specific parameters for the QueryManager */
    protected HashMap _QMParams;

    /**
     * Creates a new instance of the OspaceConfiguration
     * @param configurationFile Path to the configuration file
     */
    public OspaceConfiguration(String configurationFile)
            throws JDOMException,
            IOException,
            URISyntaxException {

        this._params =
                new HashMap();
        this._QMParams =
                new HashMap();

        SAXBuilder builder =
                new SAXBuilder();
        Document doc =
                builder.build(new File(configurationFile));
        Element root =
                doc.getRootElement();

        List<Element> params =
                root.getChildren("param");

        for (Element param : params) {
            String key =
                    param.getChild("key").getTextTrim();
            String value =
                    param.getChild("value").getTextTrim();
            // Checks for the repository URI
            if (key.equals("OSPACE_PUBLIC_REP_URI")) {
                URI uri =
                        new URI(value);
                this._params.put(key, uri);
            } else {
                this._params.put(key, value);
            }
        }

        List<Element> QMParams =
                root.getChildren("QMParam");

        for (Element QMParam : QMParams) {
            String key =
                    QMParam.getChild("key").getTextTrim();
            String value =
                    QMParam.getChild("value").getTextTrim();
            this._QMParams.put(key, value);
        }

    }

    /**
     * Gets the HashMap with the general parameters for the repository
     * @return HashMap with the general parameters for the repository
     */
    public HashMap getParams() {
        return this._params;
    }

    /**
     * Gets the HashMap with the specific parameters for the QueryManager
     * @return HashMap with the specific parameters for the QueryManager
     */
    public HashMap getQMParams() {
        return this._QMParams;
    }
}
